package pers.cnzdy.tutorial.Chapter7_Data.Section5_DataParse;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONObject;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.StringReader;
import java.util.List;

import javax.xml.parsers.SAXParserFactory;

import pers.cnzdy.tutorial.Quiz;


public class DataParse {
    public static String TAG = "DataParse";

    public static String jsonStr = "[{ " +
            "\"id\": \"a8b17025-4159-4957-9e52-cb6553018fde\", " +
            "\"A\": \"Intent\", " +
            "\"B\": \"Thread\", " +
            "\"C\": \"Service\", " +
            "\"D\": \"Layout\", " +
            "\"type\": \"MultiChoice\", " +
            "\"difficulty\": \"2\", " +
            "\"answer\": \"C\", " +
            "\"statement\": \"Android 的四大组件分别是 Activity，（     ），BroadcastReceiver 和 ContentProvider\" " +
            "}] ";
    
    public static String xmlStr = "<quizzes> " +
            "    <quiz> " +
            "        <id>a8b17025-4159-4957-9e52-cb6553018fde</id> " +
            "        <statement>Android 的四大组件分别是 Activity，（     ），BroadcastReceiver 和 ContentProvider</statement> " +
            "        <type>MultiChoice</type> " +
            "    </quiz> " +
            "</quizzes>";
    
    public static void parseXMLWithPull(String xmlData) {
        try {
            Log.d(TAG, "xml 数据：" + xmlData);

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser xmlPullParser = factory.newPullParser();
            xmlPullParser.setInput(new StringReader(xmlData));
            int eventType = xmlPullParser.getEventType();
            String id = "";
            String statement = "";
            String type = "";
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String nodeName = xmlPullParser.getName();
                switch (eventType) {

                    case XmlPullParser.START_TAG: {
                        if ("id".equals(nodeName)) {
                            id = xmlPullParser.nextText();
                        } else if ("statement".equals(nodeName)) {
                            statement = xmlPullParser.nextText();
                        } else if ("type".equals(nodeName)) {
                            type = xmlPullParser.nextText();
                        }
                        break;
                    }

                    case XmlPullParser.END_TAG: {
                        if ("quiz".equals(nodeName)) {
                            Log.d(TAG, "题号: " + id);
                            Log.d(TAG, "题目：" + statement);
                            Log.d(TAG, "题型：" + type);
                        }
                        break;
                    }
                    default:
                        break;
                }
                eventType = xmlPullParser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void parseXMLWithSAX(String xmlData) {
        try {
            Log.d(TAG, "xml Data：" + xmlData);
            SAXParserFactory factory = SAXParserFactory.newInstance();
            XMLReader xmlReader = factory.newSAXParser().getXMLReader();
            ParseHandler handler = new ParseHandler(xmlStr);
            xmlReader.setContentHandler(handler);
            xmlReader.parse(new InputSource(new StringReader(xmlData)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void parseJSONWithJSONObject(String jsonData) {
        try {
            Log.d(TAG, "JSON 数据：" + jsonData);

            JSONArray jsonArray = new JSONArray(jsonData);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String id = jsonObject.getString("id");
                String statement = jsonObject.getString("statement");
                String type = jsonObject.getString("type");

                Log.d(TAG, "题号：" + id);
                Log.d(TAG, "题目：" + statement);
                Log.d(TAG, "题型：" + type);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void parseJSONWithGSON(String jsonData) {
        Gson gson = new Gson();
        List<Quiz> appList = gson.fromJson(jsonData,
                new TypeToken<List<Quiz>>() {
                }.getType());

//        Quiz quiz = gson.fromJson(data, Quiz.class);
        for (Quiz quiz : appList) {
            Log.d(TAG, "id is " + quiz.getStatementId());
//            Log.d(TAG, "name is " + quiz.getName());
//            Log.d(TAG, "version is " + app.getVersion());
        }
    }
}



